package t20240902_udf;

import org.apache.hadoop.fs.FSDataInputStream;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AES256Util {

	public static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";

	public static CipherInputStream doDecrypt(final String key, final String iv, final FSDataInputStream fsis) {
		try {
			final byte[] secretKey = javax.xml.bind.DatatypeConverter.parseHexBinary(key);
			final byte[] initVector = javax.xml.bind.DatatypeConverter.parseHexBinary(iv);
			final Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
			cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(secretKey, "AES"),
					new IvParameterSpec(initVector, 0, cipher.getBlockSize()));
			return new CipherInputStream(fsis, cipher);
		}catch (Exception ex){

		}
		return null;
	}
}
